﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace FlickrWP7.Core
{
    public  class People
    {
        HelperDelegate helperDelegateInstance;
        public  string PeopleQueryResult {get;set;}

        public  void FindByEmail (string apiKey, string email, HelperDelegate helperDelegate)
        {
            helperDelegateInstance = helperDelegate;

            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.people.findByEmail&api_key={0}&format=json&find_email={1}",
                    apiKey, email);

            client.DownloadStringAsync(new Uri(URL));
        }

        public  void FindByUsername(string apiKey, string username, HelperDelegate helperDelegate)
        {
            helperDelegateInstance = helperDelegate;

            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.people.findByUsername&api_key={0}&format=json&username={1}",
                    apiKey, username);

            client.DownloadStringAsync(new Uri(URL));
        }

        public  void GetInfo(string apiKey, string nsid, HelperDelegate helperDelegate)
        {
            helperDelegateInstance = helperDelegate;

            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.people.getInfo&api_key={0}&format=json&user_id={1}",
                    apiKey, nsid);

            client.DownloadStringAsync(new Uri(URL));
        }

        public enum SafeSearch
        {
            Undefined = 0,
            Safe =1,
            Moderate = 2,
            Restricted = 3
        }

        public enum ContentType
        {
            Undefined =0,
            Photos = 1,
            Screenshots = 2,
            Other = 3,
            PhotosAndScreenshots = 4,
            ScreenshotsAndOther = 5,
            PhotosAndOther = 6,
            All = 7
        }

        public enum PrivacyFilter
        {
            Undefined = 0,
            Public = 1,
            PrivateVisibleToFriends = 2,
            PrivateVisibleToFamily = 3,
            PrivateVisibleToBoth = 4,
            CompletelyPrivate = 5
        }

         void GetPhotos (string apiKey, string authToken, string signature, string nsid, HelperDelegate helperDelegate,
            SafeSearch safeSearch = SafeSearch.Undefined, string minUploadDate = "", string maxUploadDate="", string minTakenDate = "",
            string maxTakenDate = "", ContentType contentType = ContentType.Undefined, PrivacyFilter privacyFilter = PrivacyFilter.Undefined,
            string[] extras = null, string perPage = "",string page="")
        {
            helperDelegateInstance = helperDelegate;

            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.people.getPhotos&api_key={0}&format=json&user_id={1}&api_sig={2}&auth_token={3}",
                    apiKey, nsid,signature,authToken);

            if (safeSearch != SafeSearch.Undefined)
            {
                URL += "&safe_search=" + ((int)safeSearch).ToString();
            }

            if (contentType != ContentType.Undefined)
            {
                URL += "&content_type=" + ((int)contentType).ToString();
            }

            if (privacyFilter != PrivacyFilter.Undefined)
            {
                URL += "&privacy_filter=" + ((int)privacyFilter).ToString();
            }

            if (minUploadDate != "")
            {
                URL += "&min_upload_date=" + minUploadDate;
            }

            if (maxUploadDate != "")
            {
                URL += "&max_upload_date=" + maxUploadDate;
            }

            if (minTakenDate != "")
            {
                URL += "&min_taken_date=" + minTakenDate;
            }

            if (maxTakenDate != "")
            {
                URL += "&max_upload_date=" + maxTakenDate;
            }

            if (extras != null)
            {
                string toAdd = "&extras=";
                for (int i = 0; i < extras.Length; i++)
                {
                    if (i != extras.Length - 1)
                        toAdd += extras[i] + ",";
                    else
                        toAdd += extras[i];
                }
                URL += toAdd;
            }

            if (page != "")
            {
                URL += "&page=" + page;
            }

            if (perPage != "")
            {
                URL += "&per_page=" + perPage;
            }

            client.DownloadStringAsync(new Uri(URL));
        }

        public  void GetPhotosOf(string apiKey, string nsid, HelperDelegate helperDelegate, string[] extras = null, string page ="", string perPage = "")
        {
            helperDelegateInstance = helperDelegate;

            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.people.getPhotosOf&api_key={0}&format=json&user_id={1}",
                    apiKey, nsid);
            
            if (extras != null)
            {
                string toAdd = "&extras=";
                for (int i = 0; i < extras.Length; i++)
                {
                    if (i != extras.Length - 1)
                        toAdd += extras[i] + ",";
                    else
                        toAdd += extras[i];
                }
                URL += toAdd;
            }

            if (page != "")
            {
                URL += "&page=" + page;
            }

            if (perPage != "")
            {
                URL += "&per_page=" + perPage;
            }
            client.DownloadStringAsync(new Uri(URL));
        }

        public  void GetPublicGroups(string apiKey, string nsid, HelperDelegate helperDelegate)
        {
            helperDelegateInstance = helperDelegate;

            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.people.getPublicGroups&api_key={0}&format=json&user_id={1}",
                    apiKey, nsid);

            client.DownloadStringAsync(new Uri(URL));
        }

         void GetPublicPhotos(string apiKey, string authToken, string signature, string nsid, HelperDelegate helperDelegate,
                SafeSearch safeSearch = SafeSearch.Undefined, string[] extras = null, string perPage = "", string page = "")
        {
            helperDelegateInstance = helperDelegate;

            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.people.getPhotos&api_key={0}&format=json&user_id={1}&api_sig={2}&auth_token={3}",
                    apiKey, nsid, signature, authToken);

            if (safeSearch != SafeSearch.Undefined)
            {
                URL += "&safe_search=" + ((int)safeSearch).ToString();
            }

            if (extras != null)
            {
                string toAdd = "&extras=";
                for (int i = 0; i < extras.Length; i++)
                {
                    if (i != extras.Length - 1)
                        toAdd += extras[i] + ",";
                    else
                        toAdd += extras[i];
                }
                URL += toAdd;
            }

            if (page != "")
            {
                URL += "&page=" + page;
            }

            if (perPage != "")
            {
                URL += "&per_page=" + perPage;
            }

            client.DownloadStringAsync(new Uri(URL));
        }

        public  void GetUploadStatus(string apiKey, string authToken, string signature, HelperDelegate helperDelegate)
        {
            helperDelegateInstance = helperDelegate;

            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.people.getUploadStatus&api_key={0}&format=json&auth_token={1}&api_sig={2}",
                    apiKey, authToken,signature);

            client.DownloadStringAsync(new Uri(URL));
        }

         void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            PeopleQueryResult = e.Result;
            helperDelegateInstance();
        }
    }
}
